<?php
namespace app\common\controller;
use think\Controller;
use base\pay\WxApi;
use base\BaseMethod;

class DbData extends Controller
{
	
	/**
    *获取列表
    * /app/dataOperate
    * 需要登录 
    * @is_need_login true
    * @method get
    */
	public function index($keyWord='',$pageIndex=1,$pageSize=20)
    {
        $json = base64_decode($keyWord);
        $param = json_decode($json,true);
        $name = $param['name'];
        $filter = $param['filter'];
        if(!$param || !$name){
           return BaseMethod::ReError('参数错误！');     
        }
        $where = array();
        foreach($param['filter'] as $v){
            $value = $v["value"];  $opera= $v["opera"]; $field = $v['field'];
            $where[] = ["$field","$opera","$value"];
        }
        try {
            try {
                $model = model($name);
            } catch (\Exception $e) {
                $model = \Db::name($name);
            }
            $model = $model->where('is_del',0);
            $model = $model->where($where);
            $model = $model->page($pageIndex,$pageSize);
            $result  = $model->all();
            return BaseMethod::ReSucess($result);    
        } catch (\Exception $e) {
            return BaseMethod::ReError($e->getMessage());    
        }
    }
    
    /**
    *添加或更新指定表的数据
    * /app/dataOperate
    * 需要登录 
    * @is_need_login true
    * @method post
    */
    public function save($keyWord='')
    {
        $json = base64_decode($keyWord);
        $param = json_decode($json,true);
        $name = $param['name'];
        if(!$param || !$name){
           return BaseMethod::ReError('参数错误！');     
        }
        try {
            $param = request()->post();
            try {
                $model = model($name);
            } catch (\Exception $e) {
                $model = \Db::name($name);
            }
            $result = $model->isUpdate( $param['id'] )->allowField(true)->save( $param );
            return BaseMethod::ReSucess($result);    
        } catch (\Exception $e) {
            return BaseMethod::ReError($e->getMessage());    
        }
    }
    
    /**
    *添加或更新指定表的数据
    * /app/dataOperate
    * 需要登录 
    * @is_need_login true
    * @method delete
    */
    public function delete($id,$keyWord='')
    {
        $json = base64_decode($keyWord);
        $param = json_decode($json,true);
        $name = $param['name'];
        if(!$param || !$name || !$id){
           return BaseMethod::ReError('参数错误！');     
        }
        try {
            try {
                $model = model($name)->get( $id );
                $model->is_del=1;
                $model->save();
            } catch (\Exception $e) {
                \Db::name($name)->where('id',$id)->update(['is_del'=>1]);
            }
            return BaseMethod::ReSucess($result);    
        } catch (\Exception $e) {
            return BaseMethod::ReError($e->getMessage());    
        }
    }
   
}
